<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 的数值 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 的数值</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-Number（数字）"><a href="#Python-Number（数字）" class="headerlink" title="Python Number（数字）"></a>Python Number（数字）</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python Number 数据类型用于存储数值。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据类型是不允许改变的，这就意味着如果改变 Number 数据类型的值，将重新分配内存空间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下实例在变量赋值时 Number 对象被创建：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">var1 = <span class="number">1</span></div><div class="line">var2 = <span class="number">10</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以使用 del 语句删除一些 Number 对象引用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;del 语句的语法是：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">del</span> var1[,var3[,var3[....,varN]]]]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过使用 del 语句删除单个或多个对象：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">del</span> var</div><div class="line"><span class="keyword">del</span> var_a, var_b</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 支持四种不同的数值类型</p>
<ul>
<li><strong>整型（int）</strong>-通常被称为是整型或整数，是正或负整数，不带小数点。</li>
<li><strong>长整型(long integers)</strong>-无限大小的整数，证书最后是一个大写或小写的L。</li>
<li><strong>浮点型（floating point real values）</strong>-浮点型由整数部分与小数部分组成，浮点型也可以使用科学计数法表示 （<code>$2.5e2=2.5x10^2=250$</code>）</li>
<li><strong>复数（complex numbers）</strong>-复数由实数部分和叙述部分构成，可以用 a + bj ,或者 complex(a,b) 表示，复数的实部 a 和虚部 b 都是浮点型。</li>
</ul>
<table>
<thead>
<tr>
<th>int</th>
<th>long</th>
<th>float</th>
<th>complex</th>
</tr>
</thead>
<tbody>
<tr>
<td>10</td>
<td>51924361L</td>
<td>0.0</td>
<td>3.14j</td>
</tr>
<tr>
<td>100</td>
<td>-0x1932L</td>
<td>15.20</td>
<td>45.j</td>
</tr>
<tr>
<td>-786</td>
<td>0122L</td>
<td>-21.9</td>
<td>9.322e-36j</td>
</tr>
<tr>
<td>080</td>
<td>0xDEFABCECBDAECBFBAEI</td>
<td>32.3+e18</td>
<td>.876j</td>
</tr>
<tr>
<td>-0490</td>
<td>535633629843L</td>
<td>-90.</td>
<td>-.6545+0j</td>
</tr>
<tr>
<td>-0x260</td>
<td>-052318172735L</td>
<td>-32.54e100</td>
<td>3e+26j</td>
</tr>
<tr>
<td>0x69</td>
<td>-472188529L</td>
<td>70.2-E12</td>
<td>4.53e-7j</td>
</tr>
</tbody>
</table>
<ul>
<li>长整型也可以使用小写 “L”，但是还是建议使用大写 “L”，避免与数字 “1” 混淆。Python 使用 “L” 来显示长整型。</li>
<li>Python 还支持复数，复数由实数部分和虚数部分构成，可以用 a + bj,或者complex(a,b)表示， 复数的实部a和虚部b都是浮点型。</li>
</ul>
<h2 id="Python-Number-类型转换"><a href="#Python-Number-类型转换" class="headerlink" title="Python Number 类型转换"></a>Python Number 类型转换</h2><table>
<thead>
<tr>
<th>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</th>
<th>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</th>
</tr>
</thead>
<tbody>
<tr>
<td>int(x [,base ])</td>
<td>将x转换为一个整数  </td>
</tr>
<tr>
<td>long(x [,base ])</td>
<td>将x转换为一个长整数  </td>
</tr>
<tr>
<td>float(x )</td>
<td>将x转换到一个浮点数  </td>
</tr>
<tr>
<td>complex(real [,imag ])</td>
<td>创建一个复数  </td>
</tr>
<tr>
<td>str(x )</td>
<td>将对象 x 转换为字符串  </td>
</tr>
<tr>
<td>repr(x )</td>
<td>将对象 x 转换为表达式字符串  </td>
</tr>
<tr>
<td>eval(str )</td>
<td>用来计算在字符串中的有效Python表达式,并返回一个对象  </td>
</tr>
<tr>
<td>tuple(s )</td>
<td>将序列 s 转换为一个元组  </td>
</tr>
<tr>
<td>list(s )</td>
<td>将序列 s 转换为一个列表  </td>
</tr>
<tr>
<td>chr(x )</td>
<td>将一个整数转换为一个字符  </td>
</tr>
<tr>
<td>unichr(x )</td>
<td>将一个整数转换为Unicode字符  </td>
</tr>
<tr>
<td>ord(x )</td>
<td>将一个字符转换为它的整数值  </td>
</tr>
<tr>
<td>hex(x )</td>
<td>将一个整数转换为一个十六进制字符串  </td>
</tr>
<tr>
<td>oct(x )</td>
<td>将一个整数转换为一个八进制字符串</td>
</tr>
</tbody>
</table>
<h2 id="Python-数学函数"><a href="#Python-数学函数" class="headerlink" title="Python 数学函数"></a>Python 数学函数</h2><table>
<thead>
<tr>
<th>函数</th>
<th>返回值（描述）</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/1.%20Python%20abs(" target="_blank" rel="external">abs(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的绝对值，如abs(-10) 返回 10</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/2.%20Pyhton%20ceil(" target="_blank" rel="external">ceil(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的上入整数，如math.ceil(4.1) 返回 5</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/3.%20Pyhton%20cmp(" target="_blank" rel="external">cmp(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>如果 x &lt; y 返回 -1, 如果 x == y 返回 0, 如果 x &gt; y 返回 1</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/4.%20Python%20exp(" target="_blank" rel="external">exp(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/5.%20Python%20fabs(" target="_blank" rel="external">fabs(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的绝对值，如math.fabs(-10) 返回10.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/6.%20Python%20floor(" target="_blank" rel="external">floor(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字的下舍整数，如math.floor(4.9)返回 4</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/7.%20Python%20log(" target="_blank" rel="external">log(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>如math.log(math.e)返回1.0,math.log(100,10)返回2.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/8.%20Python%20log10(" target="_blank" rel="external">log10(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回以10为基数的x的对数，如math.log10(100)返回 2.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/9.%20Python%20max(" target="_blank" rel="external">max(x1, x2,…)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定参数的最大值，参数可以为序列。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/10.%20Python%20min(" target="_blank" rel="external">min(x1, x2,…)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定参数的最小值，参数可以为序列。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/11.%20Python%20modf(" target="_blank" rel="external">modf(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的整数部分与小数部分，两部分的数值符号与x相同，整数部分以浮点型表示。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/12.%20Python%20pow(" target="_blank" rel="external">pow(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>x**y 运算后的值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/13.%20Python%20round(" target="_blank" rel="external">round(x [,n])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回浮点数x的四舍五入值，如给出n值，则代表舍入到小数点后的位数。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0/14.%20Python%20sqrt(" target="_blank" rel="external">sqrt(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回数字x的平方根，数字可以为负数，返回类型为实数，如math.sqrt(4)返回 2+0j</td>
</tr>
</tbody>
</table>
<h2 id="Python-随机数函数"><a href="#Python-随机数函数" class="headerlink" title="Python 随机数函数"></a>Python 随机数函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随机数可以用于数学、游戏、安全等领域中，还经常被嵌入到算法中，用以提高算法效率，并提高程序的安全性。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包含以下常用随机数函数：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/1.%20Python%20choice(" target="_blank" rel="external">choice(seq)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>从序列的元素中随机挑选一个元素，比如random.choice(range(10))，从0到9中随机挑选一个整数。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/2.%20Python%20randrange(" target="_blank" rel="external">randrange ([start,] stop [,step])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>从指定范围内，按指定基数递增的集合中获取一个随机数，基数缺省值为1</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/3.%20Python%20random(" target="_blank" rel="external">random()</a>%E5%87%BD%E6%95%B0.md)</td>
<td>随机生成下一个实数，它在[0,1)范围内。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/4.%20Python%20seed(" target="_blank" rel="external">seed([x])</a>%E5%87%BD%E6%95%B0.md)</td>
<td>改变随机数生成器的种子seed。如果你不了解其原理，你不必特别去设定seed，Python会帮你选择seed。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/5.%20Python%20shuffle(" target="_blank" rel="external">shuffle(lst)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>将序列的所有元素随机排序</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%87%BD%E6%95%B0/6.%20Pyhton%20uniform(" target="_blank" rel="external">uniform(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>随机生成下一个实数，它在[x,y]范围内。</td>
</tr>
</tbody>
</table>
<h2 id="Python-三角函数"><a href="#Python-三角函数" class="headerlink" title="Python 三角函数"></a>Python 三角函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 包括以下三角函数：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/1.%20Python%20acos(" target="_blank" rel="external">acos(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反余弦弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/2.%20Python%20asin(" target="_blank" rel="external">asin(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反正弦弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/3.%20Python%20atan(" target="_blank" rel="external">atan(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的反正切弧度值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/4.%20Python%20atan2(" target="_blank" rel="external">atan2(y, x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回给定的 X 及 Y 坐标值的反正切值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/5.%20Python%20cos(" target="_blank" rel="external">cos(x)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回x的弧度的余弦值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/6.%20Python%20hypot(" target="_blank" rel="external">hypot(x, y)</a>%E5%87%BD%E6%95%B0.md)</td>
<td>返回欧几里德范数 sqrt(x<em>x + y</em>y)。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/7.%20Python%20sin(" target="_blank" rel="external">sin(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>返回的x弧度的正弦值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/8.%20Python%20tan(" target="_blank" rel="external">tan(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>返回x弧度的正切值。</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/9.%20Python%20degrees(" target="_blank" rel="external">degrees(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>将弧度转换为角度,如degrees(math.pi/2) ， 返回90.0</td>
</tr>
<tr>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/Python%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/10.%20Python%20radians(" target="_blank" rel="external">radians(x)</a>%20%E5%87%BD%E6%95%B0.md)</td>
<td>将角度转换为弧度</td>
</tr>
</tbody>
</table>
<h2 id="Python-数学常量"><a href="#Python-数学常量" class="headerlink" title="Python 数学常量"></a>Python 数学常量</h2><table>
<thead>
<tr>
<th>常量</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>pi</td>
<td>数学常量 pi （圆周率，一般以π来表示）</td>
</tr>
<tr>
<td>e</td>
<td>数学常量 e ，e 即自然常数（自然常数）</td>
</tr>
</tbody>
</table>

	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.5 Python 的数值和字符串/6. Python 的数值/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
